package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyk.domain.Market;
import com.hyk.domain.UserFavoriteMarket;
import com.hyk.dto.R;
import com.hyk.service.MarketService;
import com.hyk.service.UserFavoriteMarketService;
import io.swagger.annotations.Api;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "用户最喜欢的交易市场")
@RequestMapping("/userFavorites")
public class UserFavoritesController {

    @Resource
    private MarketService marketService;

    @Resource
    private UserFavoriteMarketService userFavoriteMarketService;

    @PostMapping("/addFavorite")
    public R addFavorite(@RequestBody Market market){
        List<Market> list = marketService.list(new LambdaQueryWrapper<Market>()
            .eq(Market::getSymbol,market.getSymbol().toUpperCase()));
        market = list.get(0);
        Long userId = Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());

        UserFavoriteMarket userFavoriteMarket = new UserFavoriteMarket();
        userFavoriteMarket.setType(Integer.valueOf(market.getType()));
        userFavoriteMarket.setUserId(userId);
        userFavoriteMarket.setMarketId(market.getId());
        userFavoriteMarketService.save(userFavoriteMarket);

        return R.ok();
    }

}
